package com.zhibi.util.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 * 如果str为null，返回“”,否则返回str
	 * 
	 * @param str
	 * @return
	 */
	public static String toNotNull(String str) {
		if (str == null) {
			return "";
		}
		return str;
	}

	public static String toNotNull(Object o) {
		if (o == null) {
			return "";
		}
		if(o instanceof String){
			return (String)o;
		}else{
			return o.toString();
		}
	}
	
	/**
	 * 检查email是否是邮箱格式，返回true表示是，反之为否
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		email = toNotNull(email);
		Pattern regex = Pattern
				.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
		Matcher matcher = regex.matcher(email);
		boolean isMatched = matcher.matches();
		return isMatched;
	}
	
	/**
	 * 检验手机号码格式
	 * @param mobiles
	 * @return
	 */
	 public static boolean isMobile(String mobiles){     
		 Pattern p = Pattern.compile("^1[3|4|7|5|8][0-9]{9}$");     
        Matcher m = p.matcher(mobiles);      
        return m.matches();     
    } 
	
	/**
	 * 检查身份证的格式，返回true表示是，反之为否
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isCard(String cardId) {
		cardId = toNotNull(cardId);
		//身份证正则表达式(15位) 
		Pattern isIDCard1=Pattern.compile("^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"); 
		//身份证正则表达式(18位) 
		Pattern isIDCard2=Pattern.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$"); 
		Matcher matcher1= isIDCard1.matcher(cardId);
		Matcher matcher2= isIDCard2.matcher(cardId);
		boolean isMatched = matcher1.matches()||matcher2.matches();
		return isMatched;
	}

	/**
	 * 判断字符串是否为整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {
		if (isEmpty(str)) {
			return false;
		}
		Pattern regex = Pattern.compile("\\d*");
		Matcher matcher = regex.matcher(str);
		boolean isMatched = matcher.matches();
		return isMatched;
	}

	/**
	 * 判断字符串是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		if (isEmpty(str)) {
			return false;
		}

		Pattern regex = Pattern.compile("\\d*(.\\d*)?");
		Matcher matcher = regex.matcher(str);
		boolean isMatched = matcher.matches();
		return isMatched;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str)) {
			return true;
		}
		return false;
	}

	/**
	 * 首字母大写
	 * 
	 * @param s
	 * @return
	 */
	public static String firstCharUpperCase(String s) {
		StringBuffer sb = new StringBuffer(s.substring(0, 1).toUpperCase());
		sb.append(s.substring(1, s.length()));
		return sb.toString();
	}
	
	/**
	 * 隐藏后几位为*
	 * @param str
	 * @param len
	 * @return
	 */
	public static String hideLastChar(String str,int len){
		if(str==null) return null;
		char[] chars=str.toCharArray();
		if(str.length()<=len){
			for(int i=0;i<chars.length;i++){
				chars[i]='*';
			}
		}else{
			for(int i=chars.length-1;i>chars.length-len-1;i--){
				chars[i]='*';
			}
		}
		str=new String(chars);
		return str;
	}
	
	
	/**
	 * 检验用户名
	 * 规则：数字与字母组合，字母，汉字，4-16位(?![a-zA-Z]+$)
	 * @param userName
	 * @return
	 */
	public static boolean checkname(String username){
		Pattern p = Pattern.compile("^(?![0-9]+$)[0-9A-Za-z_\u0391-\uFFE5]{2,15}$");
		Matcher m = p.matcher(username);
		return m.matches();
	}
	
	/**
	 * 密码必须为数字+字母8位以上
	 * @param pwd
	 * @return
	 */
	public static boolean checkwd(String pwd){
		String regEx = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z~!@#$%^&*()]{8,16}$"; 
		Pattern pat = Pattern.compile(regEx);
		Matcher mat = pat.matcher(pwd);
		boolean rs = mat.find(); 
		return rs;
	}
	
	/**
	 * 判断密码有无字母
	 * @param pwd
	 * @return
	 */
	public static boolean pwdContainStr(String pwd){
		Pattern p = Pattern.compile("[a-z|A-Z]");
		Matcher m = p.matcher(pwd);
		return m.find();
	}
	
	/**
	 * 判断密码有无数字
	 * @param pwd
	 * @return
	 */
	public static boolean pwdContainNum(String pwd){
		Pattern p = Pattern.compile("[0-9]");
		Matcher m = p.matcher(pwd);
		return m.find();
	}
	
    
    public static boolean checkDateString(String dateStr){
		String eL= "[1-9]{1}[0-9]{3}-[0-9]{2}-[0-9]{2}";   
        Pattern p = Pattern.compile(eL);    
        Matcher m = p.matcher(dateStr);    
		return m.matches();
	}
    
    
	/**
	 * 产生协议的单号，借款单号，投标单号
	 * @param type ：单号类型
	 * @param numb ：当前数
	 * @param endNum:随机两位数
	 * 协议编号：如：HT+20130927（时间）+0013(当前订单的顺序)+09（随机数）
	 * 合同编号:HT20130927001391,投资申请号TZ130927010919,借款申请号JK130927001391
	 * @return
	 */
	public static String madeAgreementNo(String type,String date,int numb,int endNum){
		//int endNum=(int)(Math.random()*90)+10;
		if (numb<10) {
			return type+date+"000"+numb+endNum;
		}else if(numb<100){
			return type+date+"00"+numb+endNum;
		}else if (numb<1000) {
			return type+date+"0"+numb+endNum;
		}else{
			return type+date+numb+endNum;
			}
		}

		
}

